<?php
/** 
 * Created by RisingSun. 
 * User: MarkSpace 
 * Date: 20231021
 * Time: 11:05 
 * 众筹管理
 */
namespace app\settle\controller;

use  Mscore\Core\Controller;
use  app\settle\model\Recharge as RechargeModel;
use  app\settle\model\Withdraw as WithdrawModel;
use  app\settle\model\Common as CommonModel;
use  Mscore\Core\Lib\ArrayMethod;
use  app\admin\model\Login as LoginModel;

class RechargeController extends Controller{

	private $model;
    private $login;
    /**
     * 初始化自动连接方法
     * @param array $sysmod 
     * @param array $where 
    */
    public function __construct()  
    {    
        $this->model = new RechargeModel;
        $this->login = (new LoginModel)->isAuth();
        if(isset($this->login['code'])&&$this->login['code']==401){
            echo json_encode($this->login);
            Header("Location:".config('server.login_url'));
            exit;
        }
    }   

    
    /**
     * 账户列表
     * @param array $list 
     * @param array $where 
     */ 
    public function list(){  
        //文章类
        $post = post();
        switch (true) {
            case $post:
                $page = post('page',1);
                $limit = post('limit',10);
                $where = ' a.dels<1  and a.grade="agent" ';
                if($this->login['info']['ranges']<1){
                    $where .= ' and a.aid = '.$this->login['info']['aid'];
                }
                if(post('username')){
                    $where .= ' and a.username like "%'.post('username').'%"';
                }
                if(post('realname')){
                    $where .= ' and a.realname like "%'.post('realname').'%"';
                }
                if(post('mobile')){
                    $where .= ' and a.mobile like "%'.post('mobile').'%"';
                }
                if(post('roletype')){
                    $where .= ' and a.roletype = '.post('roletype');
                }
                $datalist = $this->model->list($where,$page,$limit);
                $data = [
                    "code" => 200,
                    "data" => $datalist['datalist'],
                    "count"=> $datalist['total'],
                    "msg" => 'ok'
                ];
                $this->echojson($data);
                break;
            default:
                $rolelist = (new CommonModel())->getrole('dels<1 and status>0');
                $type=!empty(get('type'))?get('type'):'';
                $this->assign('type', $type);
                $this->assign('rolelist', $rolelist);
                $this->display('list.html');
                break;
        }
    }

    /**
     * 查看资金
     * @param array $add 
     * @param array $where 
     */ 
    public function show(){
        if(get('aid')){
            $datainfo =$this->model->info(get('aid'));
        }
        $datainfo['aid']=isset($datainfo['aid'])?$datainfo['aid']:0;
        $datainfo['username']=isset($datainfo['username'])?$datainfo['username']:'';
        $datainfo['realname']=isset($datainfo['realname'])?$datainfo['realname']:'';
        $datainfo['mobile']=isset($datainfo['mobile'])?$datainfo['mobile']:'';
        $datainfo['email']=isset($datainfo['email'])?$datainfo['email']:'';
        $datainfo['cash']=isset($datainfo['cash'])?$datainfo['cash']:0;
        $datainfo['amount']=isset($datainfo['amount'])?$datainfo['amount']:0;
        $datainfo['integra']=isset($datainfo['integra'])?$datainfo['integra']:0;
        $datainfo['credit']=isset($datainfo['credit'])?$datainfo['credit']:0;
        $datainfo['language']=isset($datainfo['language'])?$datainfo['language']:'zh';

        $action = get('action');
        $this->assign('action', $action);
        $this->assign('datainfo',$datainfo);
        $this->display('recharge_list.html');
    }

    /**
     * 提现列表
     * @param array $withdraw 
     * @param array $where 
     */ 
    public function withdraw(){  
        $page = post('page',1);
        $limit = post('limit',10);
        $aid = !empty(post('id'))?post('id'):get('id');
        $where = 'a.dels<1 and a.status=2 and a.pid= '.$aid;
        if(post('time')){
            switch (post('time')) {
                case 1:
                    $where .= ' and DATE(a.createtime)="'.date("Y-m-d",time()).'"';
                    break;
                case 2:
                    $where .= ' and YEARWEEK(a.createtime) = YEARWEEK(CURRENT_DATE)';
                    break;
                case 3:
                    $where .= ' and DATE_FORMAT(a.createtime,"%Y-%m") = DATE_FORMAT(CURDATE(),"%Y-%m")';
                    break;
                default:
                    $where .='';
                    break;
            }
        }
        $datalist = (new WithdrawModel())->withdraw($where,$page,$limit);
        $nowdraw = (new WithdrawModel())->drawinfo($where,' sum(a.money) as money, sum(a.integra) as integra, sum(a.fee) as fee');
        $payment = (new CommonModel())->getBaseinfo('keyname="payment"','content')['content']; $currency='';
        if(!empty($payment)){  $currency = $payment['currency']; }
        unset($payment);
        $data = [
            "code" => 200,
            "data" => $datalist['datalist'],
            "count"=> $datalist['total'],
            "money"=> isset($nowdraw['money'])&&!empty($nowdraw['money'])?$nowdraw['money']:0,
            "integra"=> isset($nowdraw['integra'])&&!empty($nowdraw['integra'])?$nowdraw['integra']:0,
            "fee"=> isset($nowdraw['fee'])&&!empty($nowdraw['fee'])?$nowdraw['fee']:0,
            "currency"=> $currency,
            "msg" => 'ok'
        ];
        $this->echojson($data);
    }

    /**
     * 充值列表
     * @param array $rechargelist 
     * @param array $where 
     */ 
    public function rechargelist(){  
        $page = post('page',1);
        $limit = post('limit',10);
        $aid = !empty(post('id'))?post('id'):get('id');
        $where = 'a.dels<1 and a.status>2 and a.type=1 and a.pid= '.$aid;
        if(post('method')){
            $where .= ' and a.method= '.post('method');
        }
        if(post('time')){
            switch (post('time')) {
                case 1:
                    $where .= ' and DATE(a.createtime)="'.date("Y-m-d",time()).'"';
                    break;
                case 2:
                    $where .= ' and YEARWEEK(a.createtime) = YEARWEEK(CURRENT_DATE)';
                    break;
                case 3:
                    $where .= ' and DATE_FORMAT(a.createtime,"%Y%m") = DATE_FORMAT( CURDATE( ) , "%Y%m")';
                    break;
                default:
                    $where .='';
                    break;
            }
        }
        $datalist = $this->model->rechargelist($where,$page,$limit);
        $nowrecharge = $this->model->rechargeinfo($where,' sum(a.remoney) as money, sum(a.reintegra) as integra');
        $payment = (new CommonModel())->getBaseinfo('keyname="payment"','content')['content']; $currency='';
        if(!empty($payment)){  $currency = $payment['currency']; }
        unset($payment);
        $data = [
            "code" => 200,
            "data" => $datalist['datalist'],
            "count"=> $datalist['total'],
            "money"=> isset($nowrecharge['money'])&&!empty($nowrecharge['money'])?$nowrecharge['money']:0,
            "integra"=> isset($nowrecharge['integra'])&&!empty($nowrecharge['integra'])?$nowrecharge['integra']:0,
            "currency"=> $currency,
            "msg" => 'ok'
        ];
        $this->echojson($data);
    }

    /**
     * 资金查看
     * @param array $mlist
     * @param array $where 
     */ 
    public function mlist(){
        $action = get('action');
        $this->assign('action', $action);
        $this->display('mlist.html');
    }

    /**
     * 提现列表
     * @param array $mwithdraw
     * @param array $where 
     */ 
    public function mwithdraw(){  
        $page = post('page',1);
        $limit = post('limit',10);
        $where = 'a.dels<1 and a.status>2 and a.type<3';
        if($this->login['info']['ranges']<1){
            $where .= ' and (a.pid = '.$this->login['info']['aid'].' or a.aid='.$this->login['info']['aid'].')';
        }
        if(post('time')){
            switch (post('time')) {
                case 1:
                    $where .= ' and DATE(a.createtime)="'.date("Y-m-d",time()).'"';
                    break;
                case 2:
                    $where .= ' and YEARWEEK(a.createtime) = YEARWEEK(CURRENT_DATE)';
                    break;
                case 3:
                    $where .= ' and DATE_FORMAT(a.createtime,"%Y-%m") = DATE_FORMAT(CURDATE(),"%Y-%m")';
                    break;
                default:
                    $where .='';
                    break;
            }
        }
        if(post('wdtimerange')){
            $wdtimerange=explode('~', post('wdtimerange'));
            $wdtimerange[0]= date('Y-m-d 00:00:00',strtotime($retimerange[0]));
            $wdtimerange[1]= date('Y-m-d 23:59:59',strtotime($retimerange[1]));
            $where .= ' and a.createtime BETWEEN "'.$wdtimerange[0].'" AND "'.$wdtimerange[1].'"'; 
            unset($wdtimerange); 
        }
        if(post('wdusername')){
            $user = (new CommonModel())->getAccount(' username = "'.post('wdusername').'"', 'aid');
            if(!empty($user)){
                $where .= ' and a.aid='.$user['aid'];
            }else{
                $where .= ' and a.aid=0';
            }
            unset($user); 
        }
        $datalist = (new WithdrawModel())->withdraw($where,$page,$limit);
        $nowdraw = (new WithdrawModel())->drawinfo($where,' sum(a.money) as money, sum(a.integra) as integra, sum(a.fee) as fee');
        $payment = (new CommonModel())->getBaseinfo('keyname="payment"','content')['content']; $currency='';
        if(!empty($payment)){  $currency = $payment['currency']; }
        unset($payment);
        $data = [
            "code" => 200,
            "data" => $datalist['datalist'],
            "count"=> $datalist['total'],
            "money"=> isset($nowdraw['money'])&&!empty($nowdraw['money'])?$nowdraw['money']:0,
            "integra"=> isset($nowdraw['integra'])&&!empty($nowdraw['integra'])?$nowdraw['integra']:0,
            "fee"=> isset($nowdraw['fee'])&&!empty($nowdraw['fee'])?$nowdraw['fee']:0,
            "currency"=> $currency,
            "msg" => 'ok'
        ];
        $this->echojson($data);
    }

    /**
     * 充值列表
     * @param array $mrechargelist
     * @param array $where 
     */ 
    public function mrechargelist(){  
        $page = post('page',1);
        $limit = post('limit',10);
        $where = 'a.dels<1 and a.status>2 and a.type=1 ';
        if($this->login['info']['ranges']<1){
            $where .= ' and (a.pid = '.$this->login['info']['aid'].' or a.aid='.$this->login['info']['aid'].')';
        }
        if(post('method')){
            $where .= ' and a.method= '.post('method');
        }
        if(post('time')){
            switch (post('time')) {
                case 1:
                    $where .= ' and DATE(a.createtime)="'.date("Y-m-d",time()).'"';
                    break;
                case 2:
                    $where .= ' and YEARWEEK(a.createtime) = YEARWEEK(CURRENT_DATE)';
                    break;
                case 3:
                    $where .= ' and DATE_FORMAT(a.createtime,"%Y%m") = DATE_FORMAT( CURDATE( ) , "%Y%m")';
                    break;
                default:
                    $where .='';
                    break;
            }
        }
        if(post('retimerange')){
            $retimerange=explode('~', post('retimerange'));
            $retimerange[0]= date('Y-m-d 00:00:00',strtotime($retimerange[0]));
            $retimerange[1]= date('Y-m-d 23:59:59',strtotime($retimerange[1]));
            $where .= ' and a.createtime BETWEEN "'.$retimerange[0].'" AND "'.$retimerange[1].'"'; 
            unset($retimerange); 
        }
        if(post('username')){
            $user = (new CommonModel())->getAccount(' username = "'.post('username').'"', 'aid');
            if(!empty($user)){
                $where .= ' and a.aid='.$user['aid'];
            }else{
                $where .= ' and a.aid=0';
            }
            unset($user); 
        }
        $datalist = $this->model->rechargelist($where,$page,$limit);
        $nowrecharge = $this->model->rechargeinfo($where,' sum(a.remoney) as money, sum(a.reintegra) as integra');
        $payment = (new CommonModel())->getBaseinfo('keyname="payment"','content')['content']; $currency='';
        if(!empty($payment)){  $currency = $payment['currency']; }
        unset($payment);
        $data = [
            "code" => 200,
            "data" => $datalist['datalist'],
            "count"=> $datalist['total'],
            "money"=> isset($nowrecharge['money'])&&!empty($nowrecharge['money'])?$nowrecharge['money']:0,
            "integra"=> isset($nowrecharge['integra'])&&!empty($nowrecharge['integra'])?$nowrecharge['integra']:0,
            "currency"=> $currency,
            "msg" => 'ok'
        ];
        $this->echojson($data);
    }

}
